﻿
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Mail;
using System.Text;

namespace TL_Cloud.Utils
{
    /// <summary>
    /// 邮件管理 不处理异常
    /// </summary>
    public class MailHelper
    {
        private string MailServer = "";
        private int port = 25;
        private string MailUserName = "";
        private string password = "";
        private string MailName = "";
        public MailHelper(string MailServer, int port, string userName, string password,string mailName)
        { 
            this.MailServer = MailServer;
            this.port = port;
            this.MailUserName = userName;
            this.password = password;
            this.MailName = mailName;
        }

        public bool Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
        { 
            MailMessage message = new MailMessage();
            // 接收人邮箱地址
            message.To.Add(new MailAddress(to));
            message.From = new MailAddress(MailUserName, MailName);
            message.BodyEncoding = Encoding.GetEncoding(encoding);
            message.Body = body;
            //GB2312
            message.SubjectEncoding = Encoding.GetEncoding(encoding);
            message.Subject = subject;
            message.IsBodyHtml = isBodyHtml;

            SmtpClient smtpclient = new SmtpClient(MailServer, 25);
            smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, password);
            //SSL连接
            smtpclient.EnableSsl = enableSsl;
            smtpclient.Send(message);
            return true;
        }
    }
}
